package com.admin.util;

import java.io.IOException;
import java.util.Random;

import javax.servlet.http.HttpServletResponse;

public class BaseUtil {

	/**
	 * 字符串转换成Int
	 * @param str
	 * @return
	 */
	public static int stringToInt(String str,int defaultInt){
		try {
			return Integer.parseInt(str);
		} catch (NumberFormatException e) {
			return defaultInt;
		}
	}
	
	/**
	 * 字符串转换成Int
	 * @param str
	 * @return
	 */
	public static int stringToInt(String str){
		return stringToInt(str, 0);
	}
	
	/**
	 * 返回sql server的分页语句
	 * @param searchField
	 * @param keyField
	 * @param table
	 * @param condition
	 * @param start
	 * @param limit
	 * @return
	 */
	public static String getPageSqlForSqlServer(String searchField,String keyField,String table,String condition,int start,int limit){
		StringBuilder sql = new StringBuilder("select ");
		if (limit > 0) {
			sql.append("top ");
			sql.append(limit);
		}
		sql.append(' ');
		sql.append(searchField);
		sql.append(" from ");
		sql.append(table);
		if (limit > 0 || (condition != null && condition.length() > 0))
			sql.append(" where ");

		if (limit > 0) {
			sql.append(" ("+keyField+" not in (select top ");
			sql.append(start);
			sql.append(" "+keyField+" from ");
			sql.append(table);
			if(condition != null && condition.length() > 0) {
				sql.append(" where ");
				sql.append(condition);
			}
			sql.append(" order by ");
			sql.append(keyField);
			sql.append(" desc ");
			sql.append("))");
		}
		if (condition != null && condition.length() > 0) {
			sql.append(" and ");
			sql.append(condition);
		}
		sql.append(" order by ");
		sql.append(keyField);
		sql.append(" desc ");
		return sql.toString();
	}
	
	/**
	 * 生成指定长度的随机数
	 * @param pwd_len
	 * @return
	 */
	 public static String genRandomNum(int pwd_len) {  
        // 35是因为数组是从0开始的，26个字母+10个数字  
        final int maxNum = 10;  
        int i; // 生成的随机数  
        int count = 0; // 生成的密码的长度  
        char[] str = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };  
  
        StringBuffer pwd = new StringBuffer("");  
        Random r = new Random();  
        while (count < pwd_len) {  
            // 生成随机数，取绝对值，防止生成负数，  
  
            i = Math.abs(r.nextInt(maxNum)); // 生成的数最大为36-1  
  
            if (i >= 0 && i < str.length) {  
                pwd.append(str[i]);  
                count++;  
            }  
        }  
  
        return pwd.toString();  
    }
	
	 /**
	   * @Title: responseWrite
	   * @Description: Ajax Print
	   * @param response
	   * @param str
	   * @throws
	  */
	public static void responseWrite(HttpServletResponse response,Object str){
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html");
		try {
			response.getWriter().print(str);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		System.out.println("%空中信使%包月%".split("%")[2]);
	}
}
